package com.ruoyi.xdata.service.impl;

import java.util.List;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.xdata.mapper.XdataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.xdata.mapper.XdataModelMapper;
import com.ruoyi.xdata.domain.XdataModel;
import com.ruoyi.xdata.service.IXdataModelService;

import javax.annotation.PostConstruct;

/**
 * 模型Service业务层处理
 * 
 * @author yepanpan
 * @date 2024-03-29
 */
@Service
public class XdataModelServiceImpl implements IXdataModelService 
{
    public static final String CACHE_MAP = "xdata_model";
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private XdataModelMapper xdataModelMapper;
    @Autowired
    private XdataMapper xdataMapper;

    /**
     * 缓存
     * @return
     */
    @PostConstruct
    private int reRecache(){
        redisCache.deleteObject(CACHE_MAP);
        List<XdataModel> list = xdataModelMapper.selectXdataModelList(new XdataModel());
        for(XdataModel model:list){
            model.setFields(xdataMapper.selectDbTableColumns(model.getTables()));
            redisCache.setCacheMapValue(CACHE_MAP, "id_"+model.getId(), model);
            redisCache.setCacheMapValue(CACHE_MAP, model.getTables(), model);
        }
        return list.size();
    }

    /**
     * 查询模型
     *
     * @param table 数据表
     * @return 模型
     */
    @Override
    public XdataModel selectXdataModelByTable(String table){
        XdataModel model = redisCache.getCacheMapValue(CACHE_MAP, table);
        if(model != null){
            return model;
        }
        return xdataModelMapper.selectXdataModelByTable(table);
    }

    /**
     * 查询模型
     * 
     * @param id 模型主键
     * @return 模型
     */
    @Override
    public XdataModel selectXdataModelById(Long id)
    {
        XdataModel model = redisCache.getCacheMapValue(CACHE_MAP, "id_"+id);
        if(model != null){
            return model;
        }
        return xdataModelMapper.selectXdataModelById(id);
    }

    /**
     * 查询模型列表
     * 
     * @param xdataModel 模型
     * @return 模型
     */
    @Override
    public List<XdataModel> selectXdataModelList(XdataModel xdataModel)
    {
        return xdataModelMapper.selectXdataModelList(xdataModel);
    }

    /**
     * 新增模型
     * 
     * @param xdataModel 模型
     * @return 结果
     */
    @Override
    public int insertXdataModel(XdataModel xdataModel)
    {
        return xdataModelMapper.insertXdataModel(xdataModel) + reRecache();
    }

    /**
     * 修改模型
     * 
     * @param xdataModel 模型
     * @return 结果
     */
    @Override
    public int updateXdataModel(XdataModel xdataModel)
    {
        return xdataModelMapper.updateXdataModel(xdataModel) + reRecache();
    }

    /**
     * 批量删除模型
     * 
     * @param ids 需要删除的模型主键
     * @return 结果
     */
    @Override
    public int deleteXdataModelByIds(Long[] ids)
    {
        return xdataModelMapper.deleteXdataModelByIds(ids) + reRecache();
    }

    /**
     * 删除模型信息
     * 
     * @param id 模型主键
     * @return 结果
     */
    @Override
    public int deleteXdataModelById(Long id)
    {
        return xdataModelMapper.deleteXdataModelById(id) + reRecache();
    }
}
